//设计基于数据库path查询u_id，pre_id ,username
//向前切换
//  1.路径切换 2. ..切换 3. ../..  4. ~切换
//向后切换则进行dir和path 查找，找到返回路径

//已经完成 ~，.. 切换，
//需要检查路径是否可以切换
//  1.checkusr和path是否存在，不存在则返回不存在返回值

//did not need send to cilent,just update current_path


//传入参数，数据库 当前文件夹md5，目标行为，可以为长路径，下一文件夹， .. , ../.. , ~
//ret_val_t cd_m(MYSQL *db,int netfd ,char *current_path,char *target_path)
#include"head.h"

ret_cddots_t cd_twodots(int pre_id,MYSQL *db_towdots)//当前pre_id 定位查id 取出地址,再刷新currentpath
{
    MYSQL *db_td = db_towdots ;
    int qret = 0 ;

    ret_cddots_t ret;

    ret.pre_id = 0 ;
    bzero(ret.path,sizeof(ret.path)) ;

    char buf[1024] ;
    bzero(buf,sizeof(buf)) ;
    sprintf(buf,"'SELECT * FROM FileTable WHERE id = %d' ;",pre_id) ;
    qret = mysql_query(db_td,)
    if(qret != 0)
    {
        return NULL ;
    }

    MYSQL_RES *result = mysql_store_result(db_cd) ;
    MYSQL_ROW row ;
    row = mysql_fetch_row(result) ;
    ret.pre_id = atoi(row[0]) ;
    ret.path = row[5] ;

    return ret ;
}

int cd_m(MYSQL *db ,char *current_path,char *target_path)
{
    MYSQL *db_cd = db ;
    char path[1024] = {0}
    strcpy(path,current_path) ;
    int ret = 0 ;
    //ret_val_t ret_info ;
    //ret_info.check_value = 0 ;
    //memset(ret_info.path_info,0,sizeof(ret_info.path_info)) ;
    

    const char slash[] = "/" ;
    const char apostrophe_semicolon[] = "' ;"  ;
    const char two_dots[] = "..";
    const char four_dots[] = "../..";
    const char root_symbol[] = "~";

    //select md5_name to get file path ;
    char buf[1024] = "SELECT * FROM FileTable WHERE path = '" ;
    strcat(buf,current_path) ;
    strcat(buf,apostrophe_semicolon) ;
    char *sql = buf ;

    int qret = mysql_query(db_cd,sql) ;
    if(qret != 0)
    {
        // ret_info.check_value = -1 ;//-1 查询数据库失败
        // strcpy(ret_info.check_value , mysql_error(db_cd)) ;

        // return ret_info ;
        ret = -1 ;// -1 查询数据库失败

        return ret ;
    }

    MYSQL_RES *result = mysql_store_result(db_cd) ;
    MYSQL_ROW row ;
    row = mysql_fetch_row(result) ;
    //char *current_path =  row[5] ;//获得当前路径
    int current_id = atoi(row[0]) ;
    int pre_id = atoi(row[3]) ;
    char *username = row[2] ;


    if(strcmp(target_path,root_symbol) == 0)
    {
        //切换回根目录
        // strcpy(ret_info.check_value,slash) ;
        // strcpy(ret_info.path_info,row[2]) ;
        strcpy(path,slash) ;
        strcpy(path,row[2]) ;

        bzero(current_path,sizeof(current_path))  ;
        strcpy(current_path , path) ;

        ret = 1 ;
        return ret ;
    }

    else if(strcmp(target_path,two_dots) == 0)
    {
        db_cd = db ;
        //memset(ret_info.path_info,0,sizeof(ret_info.path_info)) ;
        path = cd_twodots(pre_id,db_cd) ;
        if(path != NULL)
        {
            bzero(current_path,sizeof(current_path))  ;
            strcpy(current_path , path) ;

            return 1 ;
        }
        else
        {
            return -2 ;//cd former path failed
        }
        
        // buf[1024] = "SELECT * FROM FileTable WHERE id = '" ;
        // strcat(buf,pre_id) ;
        // strcat(buf,apostrophe_semicolon) ;
        // char *sql = buf ;

        // qret = mysql_query(db_cd,sql) ;

        // if(qret != 0)
        // {
        //     ret_info.check_value = -1 ;//-1 查询数据库失败
        //     strcpy(ret_info.check_value , mysql_error(db_cd)) ;

        //     return ret_info ;
        // }

        // MYSQL_RES *result = mysql_store_result(db_cd) ;
        // MYSQL_ROW row ;
        // row = mysql_fetch_row(result) ;
        // char *current_path =  row[5] ;//获得当前路径

        // ret_info = 1 ;
        // strcpy(ret_info.path_info,row[5]) ; 

        // return ret_info
    }
    else if(strcmp(target_path,four_dots) == 0)
    {
        //调用两次two_dots
        db_cd = db ;
        ret_cddots_t ret_four ;
        ret_four.pre_id = 0 ;
        bzero(ret_four.path ,sizeof(ret_four.path)) ;

        ret_four = cd_twodots(pre_id,db_cd) ;
        if(ret_four.path != NULL)
        {
            return -2 ;//cd former path failed
        }
        ret_four = cd_twodots(ret_four.pre_id,db_cd) ;

        if(ret_four.path != NULL)
        {
            return -2 ;//cd former path failed
        }

        else
        {
            bzero(current_path,sizeof(current_path))  ;
            strcpy(current_path , path) ;

            return 1 ;
        }
    }

    else if(strcmp(target_path,current_path) == 0)
    {
        //相等路径，无需切换
        return 1 ;
    }

    else if(strcmp(target_path,current_path) < 0)
    {
        //短移动，向后移动
        // /usr usr
        if(target_path[0] == slash)
        {
            strcat(path,current_path) ;
            strcat(path,target_path) ;
        }
        else
        {
            strcat(path,current_path) ;
            strcat(path,slash) ;
            strcat(path,target_path) ;
        }
        //拼接成新的path，进行path check
        //数据库能查询到则可以通过

        sql = buf ;

    int qret = mysql_query(db_cd,sql) ;
    if(qret != 0)
    {
        ret = -3 ;// -3 不存在于数据库中

        return ret ;
    }

    else
    {
        bzero(current_path,sizeof(current_path))  ;
        strcpy(current_path , path) ;

        ret =1 ;
        return 1 ;
    }

    }
    else if(strcmp(target_path,current_path) > 0)
    {
        //长移动
        bzero(current_path,sizeof(current_path))  ;
        strcpy(current_path , path) ;
        ret = 1 ;

        return ret ;
    }
        // db_cd = db ;
        // memset(ret_info.path_info,0,sizeof(ret_info.path_info)) ;
        // buf[1024] = "SELECT * FROM FileTable WHERE id = '" ;
        // strcat(buf,pre_id) ;
        // strcat(buf,apostrophe_semicolon) ;
        // sql = buf ;

        // qret = mysql_query(db_cd,sql) ;

        // if(qret != 0)
        // {
        //     ret_info.check_value = -1 ;//-1 查询数据库失败
        //     strcpy(ret_info.check_value , mysql_error(db_cd)) ;

        //     return ret_info ;
        // }

        // MYSQL_RES *result = mysql_store_result(db_cd) ;
        // MYSQL_ROW row ;
        // row = mysql_fetch_row(result) ;
        // pre_id =  row[3] ;//获得当前前驱

        // ret_info = cd_m(MYSQL *db,char *md5_name,char *target_path)
        // ret_info = 1 ;
        // strcpy(ret_info.path_info,row[5]) ;
        
        // return ret_info
    else
    {
        ret -4 ;//非法操作
        
    }
    


}




//TODO 后续再将twodots抽为函数
// ret_val_t two_dots(MYSQL *db,char *md5_name,char *target_path)
// {
//     MYSQL *db_cd = db ;
//     memset(ret_info.path_info,0,sizeof(ret_info.path_info)) ;
//     char buf[1024] = "SELECT * FROM FileTable WHERE id = '" ;
//     strcat(buf,pre_id) ;
//     strcat(buf,apostrophe_semicolon) ;
//     char *sql = buf ;

//     qret = mysql_query(db_cd,sql) ;

//     if(qret != 0)
//     {
//         ret_info.check_value = -1 ;//-1 查询数据库失败
//         strcpy(ret_info.check_value , mysql_error(db_cd)) ;

//         return ret_info ;
//     }

//     MYSQL_RES *result = mysql_store_result(db_cd) ;
//     MYSQL_ROW row ;
//     row = mysql_fetch_row(result) ;
//     char *current_path =  row[5] ;//获得当前路径

//     ret_info = 1 ;
//     strcpy(ret_info.check_value,slash) ;
//     strcpy(ret_info.path_info,row[5]) ;
    
//     return ret_info
// }